<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="copyright" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<title>智能营销</title>
<link href="<%=path%>/favicon.ico" rel="shortcut icon">
<link href="https://cdn.jsdelivr.net/gh/bootcdn/BootCDN/ajax/libs/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" type="text/css"
	href="<%=path%>/css/marketing/public.css" />
<link rel="stylesheet" type="text/css"
	href="<%=path%>/css/marketing/activity.css" />
<link rel="stylesheet" type="text/css"
	href="<%=path%>/css/marketing/content.css" />
<link rel="stylesheet" type="text/css" href="<%=path%>/css/marketing/app.css" />
<link rel="stylesheet" type="text/css"
	href="<%=path%>/css/marketing/app-foryx.css" />
<link rel="stylesheet" type="text/css"
	href="<%=path%>/css/marketing/set-fornew.css" />
	
<link href="<%=path%>/hplus/css/bootstrap.min14ed.css?v=3.3.6" rel="stylesheet">
<link href="<%=path%>/hplus/css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet">
<link href="<%=path%>/hplus/css/plugins/iCheck/custom.css" rel="stylesheet">
<link href="<%=path%>/hplus/css/animate.min.css" rel="stylesheet">
<link href="<%=path%>/hplus/css/style.min862f.css?v=4.1.0" rel="stylesheet">  

</head>
<body class="gray-bg">
	<div class="wrapper wrapper-content animated fadeInRight">
	<div class="container-fluid">
		
		<div class="row">
			<div class="col-md-12">
			    <div class="col-md-12">
			   <div class="ibox">
			    <div class="ibox-content text-right">
			      <h3>
						<span>
						<button id="x_button_l"  type="button" class="btn btn-outline btn-default" style="margin-right:10px">取消</button>
						<!-- <button id="create_model" type="button" class="btn btn-primary">请专家设计</button> -->
						
						<button id="create_market" type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal5">完成</button>
						</span>
					</h3>
			    </div>
			   </div>   
			 </div> 
				<div class="s_hot_box fix col-md-12">

					<div style="display: flex;">
						<div style="background: #fff; flex: 4; overflow: hidden;">
							<form class="form-inline" style="padding: 30px 0 0 39px;">
								<div class="form-group"
									style="display: flex; align-items: center; font-size: 14px;">
									<label for="exampleInputName2" style="width: 80px;"><i
										style="color: red;">*</i>模板名称</label> <input type="text"
										class="form-control"
										style="width: 300px; height: 50px; border-radius: 3px;"
										id="exampleInputName2" maxlength="15" onblur="checkLength(this)">
								</div>
							</form>
							<br> <br> <br> <br>
							<div class="pad2">
								<div style="padding: 10px 0;">
									<div style="display: flex;">
										<div class="top-line"></div>
									</div>
									<div style="display: flex;">
										<div class="item-block bg1  model-height1" id="activity_one">
											<a id="activity_name_one" style="color: white"
												href="javascript:activityOne(1);">活动一</a>
											<div class="model-con">
												<br> <br>
												<p>
													<span id="activity_dimens_one" style="text-decoration-line:">0</span>
												</p>
											</div>
										</div>
										<div class="item-block line-block2">
											<div class="prism2" style="display: none">参加上一次活动</div>
										</div>
										<div class="item-block bg2  model-height1" id="activity_two">
											<a id="activity_name_two" style="color: white"
												href="javascript:activityOne(2);">活动二</a>
											<div class="date date-left2" style="display: none">立刻</div>
											<div class="model-con">
												<br> <br>
												<p>
													<span id="activity_dimens_two">0</span>
												</p>
											</div>
										</div>
										<div class="item-block"></div>
										<div class="item-block bg3  model-height1" id="activity_three">
											<a id="activity_name_three" style="color: white"
												href="javascript:activityOne(3);">活动三</a>
											<div class="date" style="display: none">30</div>
											<div class="model-con">
												<br> <br>
												<p>
													<span id="activity_dimens_three" style="text-decoration: none">0</span>
												</p>
											</div>
										</div>
									</div>
								</div>
							</div>
						</div>
						<div style="flex: 1; background: #fff; margin-left: 20px;">
							<h4
								style="background: #fff; height: 50px; line-height: 22px; color: #03a9f4;"
								id="infoName"></h4>
							<div>
								<h4>发送对象</h4>
								<form class="pad" style="padding: 0 20px 19px;">
								<label class="radio" id="all"> <input
										id="all_radio" type="radio" name="sendobj" value="0">
										全部
									</label>
									<label class="radio" id="jy-time"> <input
										id="jy-time-radio" type="radio" name="sendobj" value="1">
										交易时间
									</label>
									<div class="pad" id="jy-time-all">
										<label class="checkbox inline" id="jy-time-old"> <input
											id="old_customer" type="checkbox" name="sendobj1" value="老客户">
											老客户
										</label> <label class="checkbox inline" id="jy-time-new"> <input
											id="new_customer" type="checkbox" name="sendobj1" value="新客户">
											新客户
										</label>
									</div>
									<label class="radio" id="jypl"> <input id="jypl-radio"
										type="radio" name="sendobj" value="2"> 交易频率
									</label>
									<div class="pad" id="jypl-all">
										<label class="checkbox inline" id="jypl-gk"> <input
											id="visitor" type="checkbox" name="sendobj2" value="过客">
											过客
										</label> <label class="checkbox inline" id="jypl-ck"> <input
											id="frequenter" type="checkbox" name="sendobj2" value="常客">
											常客
										</label> <label class="checkbox mar" id="jypl-zk"> <input
											id="loyal" type="checkbox" name="sendobj2" value="忠诚客户">
											忠诚客户
										</label>
									</div>
									<label class="radio" id="jyed"> <input id="jyed-radio"
										type="radio" name="sendobj" value="3"> 交易额度
									</label>
									<div class="pad" id="jyed-all">
										<label class="checkbox inline" id="jyed-zl"> <input
											id="main" type="checkbox" name="sendobj3" value="主力">
											主力
										</label> <label class="checkbox inline" id="jyed-yb"> <input
											id="general" type="checkbox" name="sendobj3" value="一般">
											一般
										</label> <label class="checkbox mar" id="jyed-ls"> <input
											id="scattered" type="checkbox" name="sendobj3" value="零散">
											零散
										</label>
									</div>
									<label class="radio" id="lszt"> <input id="lszt-radio"
										type="radio" name="sendobj" value="4"> 流失状态
									</label>
									<div class="pad" id="lszt-all">
										<label class="checkbox inline" id="lszt-ls"> <input
											id="loss" type="checkbox" name="sendobj4" value="流失">
											流失
										</label> <label class="checkbox inline" id="lszt-fls"> <input
											id="no_loss" type="checkbox" name="sendobj4" value="非流失">
											非流失
										</label>
									</div>
								</form>
							</div>
							<div>
								<h4>发送时间</h4>
								<form class="pad">
									<label class="radio" id="fs-time-lk"> <input
										id="right_now" type="radio" name="sendtime" value="立刻发送">
										立刻
									</label> <label class="radio" id="fs-time-jsc"> <input
										id="distance_last_consume" type="radio" name="sendtime"
										value="距上次消费"> 距上一次消费 <input
										id="distance_last_consume_vaule" type="text"
										style="width: 60px; height: 30px; border-radius: 3px; border: 1px solid #ccc;"
										name="sendtimeDate">天
									</label> <label class="radio" id="fs-time-jdyc"> <input
										id="distance_first_consume" type="radio" name="sendtime"
										value="距第一次消费"> 距第一次消费 <input
										id="distance_first_consume_value" type="text"
										style="width: 60px; height: 30px; border-radius: 3px; border: 1px solid #ccc;"
										name="sendtimeDate">天
									</label> <label class="radio" id="fs-time-jsja"> <input
										id="distance_last_activity" type="radio" name="sendtime"
										value="距上级活动"> 距上级活动 <input
										id="distance_last_activity_value" type="text"
										style="width: 60px; height: 30px; margin-left: 15px; border-radius: 3px; border: 1px solid #ccc;"
										name="sendtimeDate">天
									</label> <label class="radio" id="fs-time-detail"> <input
										id="date" type="radio" style="margin-top: 15px;"
										name="sendtime" value="date"> <input type="date"
										style="width: 160px; height: 40px; border: 1px solid #ccc; border-radius: 3px;"
										name="sendtimeDate">
									</label> <label class="radio" id="fs-time-birth"> <input
										id="birthday" type="radio" name="sendtime" value="生日">
										生日
									</label>
								</form>
							</div>
							<div>
								<h4>发送方式</h4>
								<form class="pad" style="margin: 10px 0;">
									<label class="checkbox inline" id="fsfs-sms"> <input
										id="sms" type="checkbox" name="sendmethod" value="短信">
										短信
									</label> <label class="checkbox inline" id="fsfs-email"> <input
										id="email" type="checkbox" name="sendmethod" value="邮件">
										邮件
									</label> <label class="checkbox inline" id="fsfs-weibo"> <input
										id="weibo" type="checkbox" name="sendmethod" value="微博">
										微博
									</label>
								</form>
							</div>
							<div>
								<h4>行为规则</h4>
								<form class="pad">
									<label class="radio" id="xwgz-no"> <input
										id="xwgz-no-radio" type="radio" name="rules" value="无">
										无
									</label> <label class="radio" id="xwgz-cjsj"> <input
										id="xwgz-cjsj-radio" type="radio" name="rules" value="参加上级活动">
										参加上级活动
									</label> <label class="radio" id="xwgz-nocj"> <input
										id="xwgz-nocj-radio" type="radio" name="rules" value="未参加上级活动">
										未参加上级活动
									</label>
								</form>
							</div>
							<div
								style="border-top: 3px solid #eee; padding: 5px; text-align: center;">
								<button id="save" class="btn btn-info" onclick="submitInfo()">保存</button>
							</div>
						</div>
					</div>
					<!-- 活动 -->
					<div class="s_popups">
						<div class="s_number_list"  style="max-height:500px;overflow-y:auto;">
							<span><img src="<%=path%>/images/marketing/close.png" /></span>
							<ul class="s_xk_title fix">
								<li>关键字<input id="keyWord" type="text" placeholder="输入活动名称" /></li>
								<li class="s_search_btn"><i><img
										src="<%=path%>/images/marketing/icon_search.png" /></i></li>
							</ul>
							<table>
								<thead>
									<tr>
										<th colspan="2" style="text-align: center; font-size: 18px;">活动列表</th>
									</tr>
								</thead>
								<tbody id="activityList" onclick="chooseActive()">

								</tbody>
							</table>
							<div class="m_xpages" style="width:100%;margin:0;padding: 25px 0 1px 0;">
							  <div class="mian">
								<span>显示行数</span> <select><option>5</option>
									<option>10</option>
									<option>20</option></select> <span>共80条</span> <span>1/4页</span> <a
									href="javascript:;" class="prev">上一页</a> <a href="javascript:;"
									class="next">下一页</a> <span>前往</span> <input type="text" value="1"
									class="text"> <span>页</span>
							  </div>
						    </div>
							<ol class="fix" style="margin: 5% 28% 1%;">
								<li><a href="javascript:;" class="s_cancels">取消</a></li>
								<li><a href="javascript:;" id="chooseName"">确定</a></li>
							</ol>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	 
	</div>
	 <div class="s_popup_delect2">
        <div class="s_consum">
            <p id="dialog_title"><img src="<%=path%>/images/marketing/s_tick.png" /> 活动启用成功</p>
        </div>
    </div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
	<script type="text/javascript" src="<%=path%>/js/marketing/app.js"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
	<script type="text/javascript" src="<%=path%>/js/marketing/lengthlimit.js"></script>
	<script>
 	    var access_token=$.cookie('access_token');
 	    var cid=$.cookie('cid');
 	    var uid=$.cookie('uid');
 	    var startIndex=0;
 	    var pageSize=5;
 	    var keyWord;
 	    var activity_id;
 	    var activity1;
 	    var activity2;
 	    var activity3;
 	    var activityArr=new Array();
 	    var activitys;
 	    //标题
 	    var market_name;
 	    //选择的智能营销模板id
 	    var model_id=1;
 	    //智能营销模板的key
 	    var model_key="pullcustomer";
 	    //保存标记
 	    var save_tag=false;
 	    //选择活动标记
 	    var chose_activity_flag=1;
		//发送对象
		var send_object;
		//发送时间
		var send_time;
		//行为规则
		var behavior_rule;
		var sendmethod = [];
		//活动一规则保存标记
		var save_rule_tag1=false;
		//活动二规则保存标记
		var save_rule_tag2=false;
		//活动三规则保存标记
		var save_rule_tag3=false;
		//活动1保存标记
		var save_activity_tag1=false;
		//活动2保存标记
		var save_activity_tag2=false;
		//活动3保存标记
		var save_activity_tag3=false;
		
		var rule_id1;
		
		var rule_id2;
		
		var rule_id3;
		
		var activity_rule_data1=new Object();
		var activity_rule_data2=new Object();
		var activity_rule_data3=new Object();
		//标记进入此页面进行的操作  create：创建新的智能营销  edit编辑  copy复制
		var type;
		//智能营销的id
		var market_id;
		
		$(function(){
			type=getQueryString("type");
			market_id=getQueryString("market_id")
			if(StringEmpty(market_id)){
				getMarketDetail();
			}
		});
		
		
 	    function activityOne(flag){
 	    	chose_activity_flag=flag;
 		    if(flag==1){
 		    	getActivityList();
 	 		   
 				$('.s_cancel').click(function(){
 					$('.s_popup').hide();
 					$('.issue').hide();
 				});
 				$('.s_cancels').click(function(){
 					$('.s_popups').hide();
 					$('.s_number_list').hide();
 					activity_id=0;
 				});

 				$('.s_popup .issue > span').click(function(){
 					$('.s_popup').hide();
 					$('.issue').hide();
 				});
 				 $('.s_popups .s_number_list > span').click(function(){
 					$('.s_popups').hide();
 					$('.s_number_list').hide();
 				});

 				$('.s_popups').show();
 				$('.s_number_list').show();
 		    }else if(save_tag){
 		    	getActivityList();
  	 		   
 				$('.s_cancel').click(function(){
 					$('.s_popup').hide();
 					$('.issue').hide();
 				});
 				$('.s_cancels').click(function(){
 					$('.s_popups').hide();
 					$('.s_number_list').hide();
 				});

 				$('.s_popup .issue > span').click(function(){
 					$('.s_popup').hide();
 					$('.issue').hide();
 				});
 				 $('.s_popups .s_number_list > span').click(function(){
 					$('.s_popups').hide();
 					$('.s_number_list').hide();
 				});

 				$('.s_popups').show();
 				$('.s_number_list').show();
 		    }else{
 		    	showDialog("继续操作前，请确认保存好上一活动");
 		    }
		}
		
		$("#activity_one").on('click', function(){
			//if(confirm("继续操作前，请确认保存好上一活动")){
				chose_activity_flag=1;
				activity_id=activity1;
				clearAll();
				setRightRuleHtml(activity_rule_data1);
				
		    //}
		});
		
		$("#activity_two").on('click', function(){
			//if(confirm("继续操作前，请确认保存好上一活动")){
				chose_activity_flag=2;
				activity_id=activity2;
				clearAll();
				setRightRuleHtml(activity_rule_data2);
		   // }
		});
		
		$("#activity_three").on('click', function(){
			//if(confirm("继续操作前，请确认保存好上一活动")){
				chose_activity_flag=3;
				activity_id=activity3;
				clearAll();
				setRightRuleHtml(activity_rule_data3);
		    //}
		});
		
		//清空选择状态
		function clearAll(){
			$(".pad input").prop("checked",false);
			document.getElementById("distance_last_activity_value").value = "";
        	document.getElementById("distance_first_consume_value").value ="";
        	document.getElementById("distance_last_consume_vaule").value = "";
		}
		
		$("#right_now").on('click', function(){
			$("#distance_last_consume_vaule").val("");
			$("#distance_first_consume_value").val("");
			$("#distance_last_activity_value").val("");
			$("#fs-time-detail input").val("");
		});
		
		$("#birthday").on('click', function(){
			$("#distance_last_consume_vaule").val("");
			$("#distance_first_consume_value").val("");
			$("#distance_last_activity_value").val("");
			$("#fs-time-detail input").val("");
		});
		
		$("#distance_last_consume_vaule").on('click', function(){
			$("#distance_last_consume").prop("checked",true);
			
			$("#distance_first_consume_value").val(""); 
			$("#distance_last_activity_value").val("");
			$("#fs-time-detail input").val("");
		});
		$("#distance_last_consume").on('click', function(){
			$("#distance_last_consume_vaule").focus();
			
			$("#distance_first_consume_value").val("");
			$("#distance_last_activity_value").val("");
			$("#fs-time-detail input").val("");
		});
		
		$("#distance_first_consume_value").on('click', function(){
			$("#distance_first_consume").prop("checked",true);
			
			$("#distance_last_consume_vaule").val("");
			$("#distance_last_activity_value").val("");
			$("#fs-time-detail input").val("");
		});
		$("#distance_first_consume").on('click', function(){
			$("#distance_first_consume_value").focus();
			
			$("#distance_last_consume_vaule").val("");
			$("#distance_last_activity_value").val("");
			$("#fs-time-detail input").val("");
		});
		
		$("#distance_last_activity_value").on('click', function(){
			$("#distance_last_activity").prop("checked",true);
			
			$("#distance_last_consume_vaule").val("");
			$("#distance_first_consume_value").val("");
			$("#fs-time-detail input").val("");
		});
		$("#distance_last_activity").on('click', function(){
			$("#distance_last_activity_value").focus();
			
			$("#distance_last_consume_vaule").val("");
			$("#distance_first_consume_value").val("");
			$("#fs-time-detail input").val("");
		});
		
		$("#fs-time-detail input[name='sendtimeDate']").on('click', function(){
			$("#date").prop("checked",true);
			
			$("#distance_last_consume_vaule").val("");
			$("#distance_first_consume_value").val("");
			$("#distance_last_activity_value").val("");
		});
		$("#date").on('click', function(){
			$("#fs-time-detail input[name='sendtimeDate']").focus();
			
			$("#distance_last_consume_vaule").val("");
			$("#distance_first_consume_value").val("");
			$("#distance_last_activity_value").val("");
		});
		
		
		//全部
		$("#all_radio").on('click', function(){
			$("#all_radio").prop("checked",true);
			
			var chk_value =[];
			var chk_value2=[];
			$('input[name="sendobj"]:checked').each(function(){ 
				chk_value.push($(this).val()); 
				chk_value2.push(this.id);
			}); 
			customerDimensionCount(chk_value2);
			$("#jy-time-all input").prop("checked",false);
			$("#jypl-all input").prop("checked",false);
			$("#jyed-all input").prop("checked",false);
			$("#lszt-all input").prop("checked",false);
			$("#lszt-radio").prop("checked",false);
			$("#jyed-radio").prop("checked",false);
			$("#jypl-radio").prop("checked",false);
			$("#jy-time-radio").prop("checked",false);
			
			
			
		});
		
		//交易时间
		$("#jy-time").on('click', function(){
			$("#jy-time-all input").prop("checked",true);
			
			var chk_value =[];
			var chk_value2=[];
			$('input[name="sendobj1"]:checked').each(function(){ 
				chk_value.push($(this).val()); 
				chk_value2.push(this.id);
			}); 
			customerDimensionCount(chk_value2);
			
			$("#jypl-all input").prop("checked",false);
			$("#jyed-all input").prop("checked",false);
			$("#lszt-all input").prop("checked",false);
		});
		//交易时间2
		$("#jy-time-all input").on('click', function(){
			$("#jy-time input").prop("checked",true);
			var chk_value =[]; 
			var chk_value2=[];
			$('input[name="sendobj1"]:checked').each(function(){ 
				chk_value.push($(this).val()); 
				chk_value2.push(this.id);
			}); 
			customerDimensionCount(chk_value2);
			$("#jypl-all input").prop("checked",false);
			$("#jyed-all input").prop("checked",false);
			$("#lszt-all input").prop("checked",false);
		});
		
		
		//交易频率
		$("#jypl").on('click', function(){
			$("#jypl-all input").prop("checked",true);
			var chk_value =[]; 
			var chk_value2=[];
			$('input[name="sendobj2"]:checked').each(function(){ 
				chk_value.push($(this).val()); 
				chk_value2.push(this.id);
			}); 
			customerDimensionCount(chk_value2);
			
			$("#jy-time-all input").prop("checked",false);
			$("#jyed-all input").prop("checked",false);
			$("#lszt-all input").prop("checked",false);
		});
		//交易频率2
		$("#jypl-all").on('click', function(){
			$("#jypl input").prop("checked",true);
			var chk_value =[]; 
			var chk_value2=[];
			$('input[name="sendobj2"]:checked').each(function(){ 
				chk_value.push($(this).val()); 
				chk_value2.push(this.id);
			}); 
			customerDimensionCount(chk_value2);
			$("#jy-time-all input").prop("checked",false);
			$("#jyed-all input").prop("checked",false);
			$("#lszt-all input").prop("checked",false);
		});
		
		//交易额度
		$("#jyed").on('click', function(){
			$("#jyed-all input").prop("checked",true);
			var chk_value =[]; 
			var chk_value2=[];
			$('input[name="sendobj3"]:checked').each(function(){ 
				chk_value.push($(this).val()); 
				chk_value2.push(this.id);
			}); 
			customerDimensionCount(chk_value2);
			$("#jy-time-all input").prop("checked",false);
			$("#jypl-all input").prop("checked",false);
			$("#lszt-all input").prop("checked",false);
		});
		//交易额度2
		$("#jyed-all").on('click', function(){
			$("#jyed input").prop("checked",true);
			var chk_value =[]; 
			var chk_value2=[];
			$('input[name="sendobj3"]:checked').each(function(){ 
				chk_value.push($(this).val()); 
				chk_value2.push(this.id);
			}); 
			customerDimensionCount(chk_value2);
			$("#jy-time-all input").prop("checked",false);
			$("#jypl-all input").prop("checked",false);
			$("#lszt-all input").prop("checked",false);
		});
		
		//流失状态
		$("#lszt").on('click', function(){
			$("#lszt-all input").prop("checked",true);
			var chk_value =[]; 
			var chk_value2=[];
			$('input[name="sendobj4"]:checked').each(function(){ 
				chk_value.push($(this).val()); 
				chk_value2.push(this.id);
			}); 
			customerDimensionCount(chk_value2);
			$("#jy-time-all input").prop("checked",false);
			$("#jypl-all input").prop("checked",false);
			$("#jyed-all input").prop("checked",false);
		});
		//流失状态2
		$("#lszt-all").on('click', function(){
			$("#lszt input").prop("checked",true);
			var chk_value =[]; 
			var chk_value2=[];
			$('input[name="sendobj4"]:checked').each(function(){ 
				chk_value.push($(this).val()); 
				chk_value2.push(this.id);
			}); 
			customerDimensionCount(chk_value2);
			$("#jy-time-all input").prop("checked",false);
			$("#jypl-all input").prop("checked",false);
			$("#jyed-all input").prop("checked",false);
		});
		
		$("#right_now").on('click',function(){
			
		});
		
		//选择活动 确定按钮
		$("#chooseName").on('click', function(){
			
			save_tag=false;
			$("#infoName").text(nameActive);
			if(!StringEmpty(nameActive)){
				showDialog("请选择活动");
				return ;
			}
			
			
			switch (chose_activity_flag) {
			case 1:
				$("#activity_name_one").text(nameActive);
				if(activity1!=activity_id&&activity_id!=0){
					activity1=activity_id;
					save_activity_tag1=false;
				}
				break;
            case 2:
            	$("#activity_name_two").text(nameActive);
            	if(activity2!=activity_id&&activity_id!=0){
					activity2=activity_id;
					save_activity_tag2=false;
				}
				break;
            case 3:
            	$("#activity_name_three").text(nameActive);
            	if(activity3!=activity_id&&activity_id!=0){
					activity3=activity_id;
					save_activity_tag3=false;
				}
	            break;
			default:
				break;
			}
			$('.s_popups').hide();
			
		});
		//保存按钮
		$("#save").on('click', function(){
			switch (chose_activity_flag) {
			case 1:
				break;
            case 2:
            	
				break;
            case 3:
            	
	            break;
			default:
				break;
			}
			
		});
		//完成按钮
		$("#create_market").on('click', function(){
			market_name=document.getElementById("exampleInputName2").value;
			if(StringEmpty(activity1)&&StringEmpty(activity2)&&StringEmpty(activity3)&&StringEmpty(market_name)&&market_name.length<21){
				activitys=activity1+","+activity2+","+activity3;
				if(type=="edit"){
					editMarket();
				}else if(type=="copy"){
					copyMarket();
				}else{
					createMarket();
				}
			}else if(!StringEmpty(activity1)){
				showDialog("请选择活动1，并保存活动规则");
			}else if(!StringEmpty(activity2)){
				showDialog("请选择活动2，并保存活动规则");
			}else if(!StringEmpty(activity3)){
				showDialog("请选择活动3，并保存活动规则");
			}else if(!StringEmpty(market_name)){
				showDialog("请输入名称");
			}else{
				showDialog("名称长度不能超过20个字符");
			}
			
		});
		
		//取消
		$('#x_button_l').on('click', function(){
			if(type=="create"){
				var market_rules;
				if(StringEmpty(activity1)){
					activitys=activity1;
				}
				if(StringEmpty(activity1)){
					activitys=activitys+","+activity2;
				}
				if(StringEmpty(activity1)){
					activitys=activitys+","+activity3;
				}
				var market_rules;
				if(StringEmpty(activity1)){
					market_rules=rule_id1;
				}
				if(StringEmpty(activity1)){
					market_rules=market_rules+","+rule_id2;
				}
				if(StringEmpty(activity1)){
					market_rules=market_rules+","+rule_id3;
				}
				cancel(activitys, market_rules);
			}else{
				window.location.href="<%=path%>/api/bm/ui/marketList";
			}
		});
		//搜索活动
		$(".s_search_btn").on('click', function(){
			keyWord=$("#keyWord ").val();
			getActivityList();
		});
		
		/* 获取活动列表 */
		function getActivityList(){
			var url = "<%=path%>/activity/getBriefActivityList";
        	$.post(url, {
        		    "keyWord" : keyWord,
    				"startIndex" : startIndex,
    				"pageSize" : pageSize,
    				"cid" : cid,
    				"uid" : uid,
    				"access_token" : access_token,
    			}, function(data) {
    				if(data.status == 200){
    					setActivityListHtml(data.data);
    					page(data.totalCount);
    				}else{
    					showDialog(data.message);
    				}
    			});
		}
		
	
		function setActivityListHtml(data){
			var html='';
			for(var i in data){
			    html+='<tr><td><img activity_id="'+data[i].id+'" src="<%=path%>/images/marketing/s_check_no.png" class="s_fk_pic1"/></td>';
			    html+='<td>'+data[i].name+'</td></tr>';
			}
			$('#activityList').html(html);
		}
		
		function chooseActive(){
			var target = event.target;
			if(target.src){
				$('.s_fk_pic1').each(function(e){
					this.src = '<%=path%>/images/marketing/s_check_no.png';
				});
				target.src = '<%=path%>/images/marketing/s_check.png';
			}
			nameActive = $(target).closest('td').next().text();
			
		    activity_id = $(target).attr("activity_id");
			
		} 
        //保存
		function submitInfo(){ //提交信息
			var sendobject=new Object();
			var sendTime=new Object();
			
			//发送对象
			var send_object1;
			//发送时间
			var send_time1;
			//行为规则
			var behavior_rule1;
			if(!StringEmpty(activity_id)){
				showDialog("请先选择活动");
				return;
			}
			
			var sendobj = $('input:radio[name="sendobj"]:checked').val();
			var sendobjChild = [];
			
			if(sendobj==0){
				sendobjChild.push("all");
			}else{
				$('input:checkbox[name="sendobj'+sendobj+'"]:checked').each(function(){
					sendobjChild.push(this.id);
				});
			}
			var sendtime ;
			
			$('input:radio[name="sendtime"]:checked').each(function(){
				sendtime=this.id;
			});
			if($('input:radio[name="sendtime"]:checked ~ input').length > 0){
				var sendtimeChild = $('input:radio[name="sendtime"]:checked ~ input').val();
			}

			var sendmethod1 = [];
			$('input:checkbox[name="sendmethod"]:checked').each(function(){
				sendmethod1.push(this.id);
			});
			var rules = $('input:radio[name="rules"]:checked').val();
			
			if(currentObj){
				currentObj.sendobj = sendobj1;
				currentObj.sendobjChild = sendobjChild;
				currentObj.sendtime = sendtime1;
				currentObj.sendmethod = sendmethod1;
				currentObj.rules = rules;
				if(sendtimeChild){
					currentObj.sendtimeChild = sendtimeChild;
				}else{
					currentObj.sendtimeChild = '';
				}
			}
			var sendchild=sendobjChild.join(',');
			console.log("sendchild-------->"+sendchild);
			//发送对象
			switch (sendobj) {
			    case "0":
			    	sendobject.all=sendchild;
				   break;
			    case "1":
			    	sendobject.trading_hours=sendchild;
				   break;
                case "2":
                	sendobject.trading_frequency=sendchild;
				   break;
                case "3":
                	sendobject.trading_amount=sendchild;
	               break;
                case "4":
                	sendobject.loss_status=sendchild;
	               break;
			     default:
				   break;
			}
			
		    //发送时间
			switch (sendtime) {
			    case "right_now":
			    	sendTime.right_now=1;
				     break;
			    case "distance_last_consume":
			    	sendTime.distance_last_consume=sendtimeChild;
					 break;
			    case "distance_first_consume":
			    	sendTime.distance_first_consume=sendtimeChild;
					 break;
			    case "distance_last_activity":
			    	sendTime.distance_last_activity=sendtimeChild;
					 break;
			    case "date":
			    	sendTime.date=sendtimeChild;
			    	break;
			    case "birthday":
			    	sendTime.birthday="1";
					 break;
			    default:
				   break;
			}
		    switch (rules) {
		        case "无":
		        	
		        	behavior_rule1="0";
				     break;
                case "参加上级活动":
                	behavior_rule1="1";
				     break;
                case "未参加上级活动":
                	behavior_rule1="2";
	                 break;
			    default:
				     break;
			}
	 		send_time1=JSON.stringify(sendTime);
			send_object1=JSON.stringify(sendobject);
			console.log("send_object1-------->"+send_object1);
			
	/* 		send_time1=sendTime;
			send_object1=sendobject; */
			//判断用户填写的内容
			if(StringEmpty(send_time1)&&StringEmpty(sendmethod1)&&StringEmpty(behavior_rule1)&&StringEmpty(send_object1)){
				send_time=send_time1;
				sendmethod=sendmethod1;
				behavior_rule=behavior_rule1;
				send_object=send_object1;
				switch (chose_activity_flag) {
				case 1:
					//将填写的数据封装
					activity_rule_data1.send_object=send_object;
					activity_rule_data1.send_type=sendmethod.join(',');;
					activity_rule_data1.behavior_rule=behavior_rule;
					activity_rule_data1.send_time=send_time;
					activity_rule_data1.name=nameActive;
					
					
					//判断是否保存
					if(save_activity_tag1){
						editRule(rule_id1);
					}else{
						createMarketActivity();
					}
					break;
	            case 2:
	            	activity_rule_data2.send_object=send_object;
					activity_rule_data2.send_type=sendmethod.join(',');;
					activity_rule_data2.behavior_rule=behavior_rule;
					activity_rule_data2.send_time=send_time;
					activity_rule_data2.name=nameActive;
	            	if(save_activity_tag2){
	            		editRule(rule_id2);
					}else{
						createMarketActivity();
					}
					break;
	            case 3:
	            	activity_rule_data3.send_object=send_object;
					activity_rule_data3.send_type=sendmethod.join(',');;
					activity_rule_data3.behavior_rule=behavior_rule;
					activity_rule_data3.send_time=send_time;
					activity_rule_data3.name=nameActive;
	            	if(save_activity_tag3){
	            		editRule(rule_id3);
					}else{
						createMarketActivity();
					}
		            break;
				default:
					break;
				}
			}else if(!StringEmpty(send_object1)){
				showDialog("请选择发送对象");
			}else if(!StringEmpty(send_time1)){
				showDialog("请选择发送时间");
			}else if(!StringEmpty(sendmethod1)){
				showDialog("请选择发送方式");
			}else if(!StringEmpty(behavior_rule1)){
				showDialog(behavior_rule1);
				showDialog("请选择行为规则");
			}else{
			}

		}
		//获取智能营销详情
		function getMarketDetail(){
			var url = "<%=path%>/api/smart_market/getMarketDetail";
        	$.post(url, {
        		    "cid" : cid,
    				"access_token" : access_token,
    				"uid" : uid,
    				"market_id" : market_id,
    			}, function(data) {
    				
    				save_tag=true;
    				if(data.status == 200){
    					//获取成功后设置页面html
    					var activityDatas=data.data.activityData;
    					/* $("#exampleInputName2").val(data.name); */
    					document.getElementById("exampleInputName2").value = data.data.name;
    					//解析智能营销活动数据 设置显示信心
    					for(var i=0;i<activityDatas.length;i++){
    						switch (i) {
							case 0:
							    $("#activity_name_one").text(activityDatas[i].name);
								rule_id1=activityDatas[i].rule_id;
								activity1=activityDatas[i].id;
								activity_rule_data1.send_object=activityDatas[i].send_object;
	    						activity_rule_data1.send_type=activityDatas[i].send_type;
	    						activity_rule_data1.behavior_rule=activityDatas[i].behavior_rule;
	    						activity_rule_data1.send_time=activityDatas[i].send_time;
	    						activity_rule_data1.name=activityDatas[i].name;
	    						setRightRuleHtml(activity_rule_data1);
	    						activity_id=activity1;
	    						chose_activity_flag=1;
	    						save_activity_tag1=true;
								break;
                            case 1:
                            	 $("#activity_name_two").text(activityDatas[i].name);
                            	rule_id2=activityDatas[i].rule_id;
								activity2=activityDatas[i].id;
                            	activity_rule_data2.send_object=activityDatas[i].send_object;
        						activity_rule_data2.send_type=activityDatas[i].send_type;
        						activity_rule_data2.behavior_rule=activityDatas[i].behavior_rule;
        						activity_rule_data2.send_time=activityDatas[i].send_time;
        						activity_rule_data2.name=activityDatas[i].name;
        						save_activity_tag2=true;
								break;
                            case 2:
                            	 $("#activity_name_three").text(activityDatas[i].name);
                            	rule_id3=activityDatas[i].rule_id;
								activity3=activityDatas[i].id;
                            	activity_rule_data3.send_object=activityDatas[i].send_object;
        						activity_rule_data3.send_type=activityDatas[i].send_type;
        						activity_rule_data3.behavior_rule=activityDatas[i].behavior_rule;
        						activity_rule_data3.send_time=activityDatas[i].send_time;
        						activity_rule_data3.name=activityDatas[i].name;
        						save_activity_tag3=true;
	                            break;
							default:
								break;
							}
    					}
    				}else{
    					showDialog(data.message);
    				}
    				
    		});
		}
        
        
		/* 创建智能营销活动 */
		function createMarketActivity() {
			//发起请求
			var url = "<%=path%>/api/smart_activity/createActivity";
        	$.post(url, {
        		    "cid" : cid,
    				"access_token" : access_token,
    				"uid" : uid,
    				"activity_id" : activity_id,
    			}, function(data) {
    				
    				if(data.status == 200){
    					var activityId=data.activity_id;
    					switch (chose_activity_flag) {
    					case 1:
    						save_activity_tag1=true;
    						activity1=activityId;
    						break;
    		            case 2:
    		            	save_activity_tag2=true;
    		            	activity2=activityId;
    						break;
    		            case 3:
    		            	save_activity_tag3=true;
    		            	activity3=activityId;
    			            break;
    					default:
    						break;
    					}
    					createRule(activityId);
    				}else{
    					showDialog(data.message);
    				}
    				
    		});  
		}
		
		/* 创建营销规则 */
		function createRule(activity_id) {
			var send_type=sendmethod.join(',');
			//发起请求
			var url = "<%=path%>/api/smart_activity/createRule";
        	$.post(url, {
        		    "cid" : cid,
    				"access_token" : access_token,
    				"uid" : uid,
    				"activity_id" : activity_id,
    				"send_time" : send_time,
    				"send_type" : send_type,
    				"behavior_rule" : behavior_rule,
    				"send_object" : send_object,
    			}, function(data) {
    				
    				save_tag=true;
    				showDialog("创建活动规则成功");
    				if(data.status == 200){
    					switch (chose_activity_flag) {
    					case 1:
    						save_rule_tag1=true;
    						rule_id1=data.rule_id;
    						break;
    		            case 2:
    		            	save_rule_tag2=true;
    		            	rule_id2=data.rule_id;
    						break;
    		            case 3:
    		            	save_rule_tag3=true;
    		            	rule_id3=data.rule_id;
    			            break;
    					default:
    						break;
    					}
    				}else{
    					showDialog(data.message);
    				}
    				
    		});  
		}
		/* 编辑活动规则 */
		function editRule(rule_id){
			url = "<%=path%>/api/smart_activity/editRule";
			var send_type=sendmethod.join(',');
        	$.post(url, {
        		    "cid" : cid,
    				"access_token" : access_token,
    				"uid" : uid,
    				"rule_id" : rule_id,
    				"send_time" : send_time,
    				"send_type" : send_type,
    				"behavior_rule" : behavior_rule,
    				"send_object" : send_object,
    			}, function(data) {
    				
    				save_tag=true;
    				showDialog("保存活动规则成功");
    				if(data.status == 200){
    					
    				}else{
    					showDialog(data.message);
    				}
    				
    		});
			
		}
		
		
		
		/* 更新活动 */
		function editActivity() {
			
		}
		
		/* 创建营销实例 */
		function createMarket() {
			var url = "<%=path%>/api/smart_market/create";
        	$.post(url, {
        		    "cid" : cid,
    				"access_token" : access_token,
    				"uid" : uid,
    				"name" : market_name,
    				"model_id" : model_id,
    				"model_key" : model_key,
    				"activitys" : activitys,
    			}, function(data) {
    				
    				if(data.status == 200){
    					showDialog("创建成功");
    					window.location.href="<%=path%>/api/bm/ui/marketList";
    				}else{
    					showDialog(data.message);
    				}
    				
    		});
		}
		function copyMarket() {
			var url = "<%=path%>/api/smart_market/copy";
        	$.post(url, {
        		    "cid" : cid,
    				"access_token" : access_token,
    				"uid" : uid,
    				"name" : market_name,
    				"model_id" : model_id,
    				"model_key" : model_key,
    				"activitys" : activitys,
    			}, function(data) {
    				
    				if(data.status == 200){
    					showDialog("创建成功");
    					window.location.href="<%=path%>/api/bm/ui/marketList";
    				}else{
    					showDialog(data.message);
    				}
    				
    		});
		}
		
		/* 编辑营销实例 */
		function editMarket() {
			var url = "<%=path%>/api/smart_market/edit";
        	$.post(url, {
        		    "cid" : cid,
    				"access_token" : access_token,
    				"uid" : uid,
    				"name" : market_name,
    				"market_id" : market_id,
    				"activitys" : activitys,
    			}, function(data) {
    				
    				if(data.status == 200){
    					showDialog("编辑成功");
    					window.location.href="<%=path%>/api/bm/ui/marketList";
    				}else{
    					showDialog(data.message);
    				}
    				
    		});
			
		}
		//取消
		function cancel(activitys,market_rules) {
			var url = "<%=path%>/api/smart_market/cancel";
        	$.post(url, {
        		    "cid" : cid,
    				"access_token" : access_token,
    				"uid" : uid,
    				"activitys" : activitys,
    				"market_rules" : market_rules,
    			}, function(data) {
    				
    				if(data.status == 200){
    					window.location.href="<%=path%>/api/bm/ui/marketList";
    				}else{
    					window.location.href="<%=path%>/api/bm/ui/marketList";
    				}
    				
    		});
		}
		
		
		///设置右边规则显示
		function setRightRuleHtml(data){
			$("#infoName").text(data.name);
			
			//发送对象
			var sendObjc=JSON.parse(data.send_object);
			//發送时间
			var sendTime=JSON.parse(data.send_time);
			//发送方式
			var sendType=data.send_type;
			//行为规则
			var behaviorRule=data.behavior_rule+"";
			var sendObject=[];
			
			/* 设置发送对象 */
			for (var key in sendObjc)
		    {
		        var splitValue=sendObjc[key].split(",");
		        switch (key) {
		        case "all":
		        	$("#all_radio").prop("checked",true);
		        	sendObject.push("all_radio");
		        	break;
				case "trading_hours":
					$("#jy-time-radio").prop("checked",true);
					for(var i in splitValue){
						sendObject.push(splitValue[i]);
						if(splitValue[i]=="old_customer"){
							$("#old_customer").prop("checked",true);
						}else if(splitValue[i]=="new_customer"){
							$("#new_customer").prop("checked",true);
						}
					}
					 break;
					
                case "trading_frequency":
                	$("#jypl-radio").prop("checked",true);
                	/* $("#jypl-all input").prop("checked",true); */
                	for(var i in splitValue){
                		sendObject.push(splitValue[i]);
						if(splitValue[i]=="visitor"){
							$("#visitor").prop("checked",true);
						}else if(splitValue[i]=="frequenter"){
							$("#frequenter").prop("checked",true);
						}else if(splitValue[i]=="loyal"){
							$("#loyal").prop("checked",true);
						}
					}
					 break;
                case "trading_amount":
                	$("#jyed-radio").prop("checked",true);
                	/* $("#jyed-all input").prop("checked",true); */
                	for(var i in splitValue){
                		console.log("trading_amount----->"+i);
                		sendObject.push(splitValue[i]);
						if(splitValue[i]=="main"){
							$("#main").prop("checked",true);
						}else if(splitValue[i]=="general"){
							$("#general").prop("checked",true);
						}else if(splitValue[i]=="scattered"){
							$("#scattered").prop("checked",true);
						}
					}
	
	                 break;
                case "loss_status":
                	$("#lszt-radio").prop("checked",true);
                	/* $("#lszt-all input").prop("checked",true); */
                	for(var i in splitValue){
                		sendObject.push(splitValue[i]);
                		if(splitValue[i]=="loss"){
                    		$("#loss").prop("checked",true);
                    	}else if(splitValue[i]=="no_loss"){
                    		$("#no_loss").prop("checked",true);
                    	}
                	}
	                 break;

				default:
					break;
				}
		        customerDimensionCount(sendObject);
		        
		    }
			//设置发送时间
			for(var j in sendTime){
				switch (j) {
				case "right_now":
					$("#right_now").prop("checked",true);
					break;
                case "distance_last_consume":
                	$("#distance_last_consume").prop("checked",true);
                	document.getElementById("distance_last_consume_vaule").value = sendTime[j];
                	
					break;
                case "distance_first_consume":
                	$("#distance_first_consume").prop("checked",true);
                	document.getElementById("distance_first_consume_value").value = sendTime[j];
	                break;
                case "distance_last_activity":
                	$("#distance_last_activity").prop("checked",true);
                	document.getElementById("distance_last_activity_value").value = sendTime[j];
              
	                break;
                case "date":
                	$("#date").prop("checked",true);
                	$("#fs-time-detail input[name='sendtimeDate']").val(sendTime[j]);
	                break;
                case "birthday":
                	$("#birthday").prop("checked",true);
	                break;

				default:
					break;
				}
			}
			/* 设置发送方式 */
			var splitSendType=sendType.split(",");
			for(var k in splitSendType){
				switch (splitSendType[k]) {
				case "email":
					$("#email").prop("checked",true);
					break;
                case "weibo":
                	$("#weibo").prop("checked",true);
					break;
                case "sms":
                	$("#sms").prop("checked",true);
	                break;

				default:
					break;
				}
			}
			//设置行为规则
			switch (behaviorRule) {
			case "0":
				$("#xwgz-no-radio").prop("checked",true);
				break;
            case "1":
            	$("#xwgz-cjsj-radio").prop("checked",true);
				break;
            case "2":
            	$("#xwgz-nocj-radio").prop("checked",true);
	            break;

			default:
				break;
			}
			
			
		}
		
		function customerDimensionCount(dimens){
			var url = "<%=path%>/api/smart_market/customerDimensionCount";
			var send_object=dimens.join(',');
        	$.post(url, {
        		    "cid" : cid,
    				"access_token" : access_token,
    				"uid" : uid,
    				"send_object" : send_object,
    			}, function(data) {
    				
    				if(data.status == 200){
    					switch (chose_activity_flag) {
    					
    					case 1:
    						$("#activity_dimens_one").html(data.data);
    						activity_rule_data1.demens=data.data;
    						break;
                        case 2:
                        	$("#activity_dimens_two").html(data.data);
                        	activity_rule_data2.demens=data.data;
    						break;
                        case 3:
                        	$("#activity_dimens_three").html(data.data);
                        	activity_rule_data3.demens=data.data;
    	                    break;
    					default:
    						break;
    					}
    				}
    				
    		});
		}
		
		//判断字符串是否为空
		function StringEmpty(content) {
			if (content == undefined || content == "" || content == null) {
				return false;
			} else {
				return true;
			}
		}
		
		//获取参数
		function getQueryString(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
			var r = window.location.search.substr(1).match(reg);
			if (r != null)
				return unescape(r[2]);
			return null;
		}
	    function showDialog(title){
			$('#dialog_title').html('<img src="<%=path%>/images/marketing/s_tick.png"/>'+title+''); 
			$('.s_popup_delect2,.s_consum').show().delay(2000).fadeOut();
		}
	    
	    $(".m_xpages select").change(function(){
	 		pageSize = Number($(this).val());		 		
	 		getActivityList();
	 	});
	 	
	 	$(".m_xpages a.prev").click(function(){	 		
	 		if(currentPage > 1){
	 			currentPage--;
	 			startIndex -= pageSize;	 	
	 			getActivityList();
	 		}
	 	});
	 	
	 	$(".m_xpages a.next").click(function(){
	 		if(currentPage < pageCount){
	 			currentPage++;
	 			startIndex += pageSize;	
	 			getActivityList();
	 		}
	 	});
	 	
	 	$(".m_xpages :text").keyup(function(e){
			if(e.keyCode==13){
				var pageGo = $(this).val();
				if(/^[0-9]+$/.test(pageGo)){
					pageGo = Number(pageGo);
					if(pageGo<1 || pageGo>pageCount){
						showDialog('请输入合法页数');
					}else if(pageGo != currentPage){
						startIndex = (pageGo -1) * pageSize ;	
						getCardList();
					}
				}else{
					showDialog('请输入合法页数');
				}
			}
		});
	function page(totalCount) {
		pageCount = totalCount/pageSize;
		if (totalCount % pageSize > 0)
			pageCount++;
		pageCount = parseInt(pageCount);
		currentPage =  startIndex / pageSize + 1;
		currentPage = parseInt(currentPage);
		if(currentPage == 1){
			$(".prev").addClass("pageDisable");
		}else{
			$(".prev").removeClass("pageDisable");
		}
		
		if(currentPage == pageCount || pageCount == 0){
			$(".next").addClass("pageDisable");
		}else{
			$(".next").removeClass("pageDisable");
		}
		
		$(".m_xpages>.mian>span:eq(1)").text('共'+totalCount+'条');
		$(".m_xpages>.mian>span:eq(2)").text(currentPage+'/'+pageCount+'页');
	}
	    
	    
	</script>

	<script type="text/javascript">
	
</script>
</body>
</html>